package com.atguigu.day26.dml02;

import com.atguigu.day26.problem03.*;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/*

jdbc 进行增删改操作
 */
public class IDUTest {

    static Connection connection;
    static{
        connection = DruidTest.JDBCUtils.getConnection();
    }

    @Test
    public void test03() throws Exception{
        //todo 更新
        //1.注册驱动
        //Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        //Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/atguigu", "root", "root");
        //3.准备sql
        String sql = "update t_account set balance = 9999 where aname = '杜甫'";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.执行命令获取结果
        int i = pst.executeUpdate();
        //6.展示结果
        System.out.println(i>0?"success":"fail");
        //7.关闭资源
        pst.close();
        connection.close();
    }

    @Test
    public void test02() throws Exception{
        //todo  删除
       /* //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/atguigu", "root", "root");*/
        //Connection connection = DruidTest.JDBCUtils.getConnection();
        //3.准备sql
        String sql = "delete from t_account where aname ='妲己'";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        //5.执行命令获取结果
        int i = pst.executeUpdate();
        //6.输出结果
        System.out.println(i>0?"成功":"失败");
        //7.关闭资源
        pst.close();
        connection.close();



    }



    @Test
    public void test01() throws Exception{
        //todo 增加
//        //1.注册驱动
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        //2.获取连接
//        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/atguigu", "root", "root");
        //Connection connection = JDBCUtils.getConnection();
        //3.准备sql
        String sql = "insert into t_account values(null,?,?)";
        //4.创建命令发送器
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setObject(1,"刘亦菲");
        pst.setObject(2,99999);
        //5.执行命令获取结果
        int i = pst.executeUpdate();
        //6.输出结果
        System.out.println("i = " + i);
        System.out.println(i>0?"成功":"失败");
        //7.关闭资源
        pst.close();
        connection.close();

    }
}
